package com.tian.print.http.error;

import com.google.gson.JsonParseException;

import org.json.JSONException;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.text.ParseException;

/**
 * Description:
 * <br>Author：tian
 * <br>Time: 2019/5/24 11:37
 */
public class ExceptionHandler {

    /**
     * 位置错误
     */
    public final static int UNKNOWN = 1000;

    /**
     * 解析错误
     */
    public final static int PARSE_ERROR = 1001;

    /**
     * 网络错误
     */
    public final static int NETWORK_ERROR = 1002;

    /**
     * 网络协议错误
     */
    public final static int HTTP_PROTOCAL_ERROR = 1003;

    public static ApiException handleException(Throwable e) {
        ApiException ex;
        if (e instanceof JsonParseException
                || e instanceof JSONException
                || e instanceof ParseException) {
            //解析错误
            ex = new ApiException(PARSE_ERROR, e.getMessage());
            return ex;
        } else if (e instanceof ConnectException) {
            //网络错误
            ex = new ApiException(NETWORK_ERROR, e.getMessage());
            return ex;
        } else if (e instanceof UnknownHostException || e instanceof SocketTimeoutException) {
            //连接错误
            ex = new ApiException(NETWORK_ERROR, e.getMessage());
            return ex;
        } else {
            //未知错误
            ex = new ApiException(UNKNOWN, e.getMessage());
            return ex;
        }
    }


}
